package com.ebolib.net;

import android.app.Dialog;

import com.ebo.commonlib.utils.I_Share;
import com.ebo.corelib.http.RxHttpUtils;
import com.ebo.corelib.http.base.BaseObserver;
import com.ebo.corelib.http.http.RetrofitClient;
import com.ebo.corelib.http.http.SingleRxHttp;
import com.ebo.corelib.http.interceptor.Transformer;
import com.ebo.corelib.http.upload.UploadRetrofit;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Administrator on 2018/4/24.
 */

public class RxNetUtils extends RxHttpUtils {


    public static SingleRxHttp getSInstance() {
        SingleRxHttp rxhttp = SingleRxHttp.getInstance();

        if(I_Share.isLogin()){
            Map<String,Object> paras = new HashMap<>();
            paras.put("token", I_Share.readAccountToken());
            paras.put("uid", I_Share.readAccountUserid());
            rxhttp.addParams(paras);
        }

        rxhttp.baseUrl(rxhttp.getBaseUrl() + I_Share.getLanguageType_String2());
        return rxhttp;
    }

    public static <T> T getApi(Class<T> t) {
        return RxNetUtils
                .getSInstance()
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())

                .createSApi(t);
    }


    public static <T> T getUploadApi(Class<T> t) {
        String baseUrl = RetrofitClient.getInstance().getRetrofit().baseUrl().toString() + I_Share.getLanguageType_String2();
        return UploadRetrofit
                .getInstance(baseUrl)
                .getRetrofit()
                .create(t);
    }

    public static MultipartBody.Part getMultiBody(String key, String filePath) {
        File file = new File(filePath);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData(key, file.getName(), requestFile);
        return body;
    }




    public static <T> void request(Observable<T> o, BaseObserver<T> ob, Dialog dialog) {
        o.compose(Transformer.<T>switchSchedulers(dialog))
                .subscribe(ob);
    }



    public static <T> void request(Observable<T> o, BaseObserver<T> ob) {
        o.compose(Transformer.<T>switchSchedulers())
                .subscribe(ob);
    }

    public static <T> void upload(Observable<T> o, BaseObserver<T> ob) {
        o.compose(Transformer.<T>switchSchedulers())
                .subscribe(ob);
    }

    public static <T> Disposable timer(final Observable<T> o, final BaseObserver<T> ob) {

        Disposable disposable = Flowable.interval(I_Share.getRefreshRate()*1000, TimeUnit.MILLISECONDS).doOnNext(new Consumer<Long>() {
            @Override
            public void accept(Long aLong) throws Exception {

                request(o, ob);
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {
            @Override
            public void accept(@NonNull Long aLong) throws Exception {

            }
        });

        return disposable;

    }


}
